// pages/member/order/return/return.js
import { PNT, setNavBarTitle, showToast, pushPagePath, scrollLoadList } from "../../../../utils/utils";
import { GetReturnInfo,AddReturnGoods} from "../../../../utils/apis";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    order:{
      thumb:'https://www.donglongshangshi.com/data/gallery_album/0/thumb_img/1566771634649765785.jpg',
      name:'所得到的哒哒哒哒哒哒多多多s哒哒哒哒哒哒多多多多多多多多多多多多多多多多反反复复付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付嘎嘎嘎嘎嘎过过过过过过过过过',
      price:100,
      geshu:22,
      shuxin:[
        {
          name:'机身颜色',
          gz:'红色',
        },
        {
          name:'内存',
          gz:'16G',
        }
      ]
    },
    idx: '1',
    applyList: [
      { Item_id: 1, Item_Name: "退货" },
        { Item_id:2, Item_Name: "换货" },
       
    ],
    select: false,
    tihuoWay: '请选择',
    num: 1,
    imagesList:[
      'http://www.donglongshangshi.com/data/gallery_album/0/thumb_img/1566771634649765785.jpg',
      'http://www.donglongshangshi.com/data/gallery_album/0/thumb_img/1566771634649765785.jpg',
    ],
    imgs:[],
    question:'',
    liuyan:'',
    reason:0,
    returntype:1,

    
  },
  // 单选
  selectApply: function (e) {
    let id = e.target.dataset.id
    this.setData({
      idx: id,
      returntype:id,
    })
  },
  // 下拉
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },

  mySelect(e) {
    var name = e.currentTarget.dataset.name
    var id = e.currentTarget.dataset.id
    this.setData({
      tihuoWay: name,
      select: false,
      reason:id,
    })
  },
  // 
  /* 加数 */
  addCount: function (e) {
    console.log("刚刚您点击了加1");
    var num = this.data.num;
    // 总数量-1  
    if (num < this.data.returndata.goods[0].goods_number) {
      this.data.num++;
    }
    // 将数值与状态写回  
    this.setData({
      num: this.data.num
    });
  },
  /* 减数 */
  delCount: function (e) {
    console.log("刚刚您点击了减1");
    var num = this.data.num;
    // 商品总数量-1
    if (num > 1) {
      this.data.num--;
    }
    // 将数值与状态写回  
    this.setData({
      num: this.data.num
    });
  },
  // 上传图片
  upImg: function (e) {
    var that = this;
    if(this.data.imgs.length >=5){
      wx.showToast({
        title: '最多上传五张图片',
        icon:'warning',
        image:'/images/icon_toast-warning.png'
      })
      return;
    }
    wx.chooseImage({
      count: 1, // 默认最多一次上传9张图片
      sizeType: ['original', 'compressed'], // 允许原图和压缩图
      sourceType: ['album', 'camera'], // 允许相册和相机
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 500
        })
        wx.uploadFile({
          url: 'https://www.donglongshangshi.com/wechatapi/index.php/wechatapi/order/comment_img', //服务器上传接口
          filePath: tempFilePaths[0], //文件资源路径
          name: 'file',
          header: {
            'Content-Type': 'Application/json'
          },
          success(res) {
            console.log(res)
            var data = JSON.parse(res.data)
            console.log(data)
            if (data.error_code == 10000) {
              var imgs = that.data.imgs;
              imgs.push(data.data);
              that.setData({
                imgs: imgs,
              })
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    setNavBarTitle(PNT.order.main);
    this.loginModal = this.selectComponent("#login-modal");
    if(!this.loginModal.check()){
      return false;
    }
    var info = wx.getStorageSync('userInfo')
    this.setData({
      orderId: opt.orderId || 0,
      info: info
    });
    this.getReturnInfo();
  },

   /**
   * 获取页面内容信息
   */
  getReturnInfo(){
    var data = {};
    let  self = this;
    data.order_id = this.data.orderId;
    data.user_id = this.data.info.user_id;
    GetReturnInfo(data).then(res=>{
      if(res.error_code == 10000){
        self.setData({
          returndata:res.data,
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon:"warning",
        })
      }
    })
  },
  /**
   * 输入值
   */
  inputvalue(e){
    var self = this;
    var item = e.currentTarget.dataset;
    if(item.name == 'question'){
      self.setData({
        question:e.detail.value
      })
    }
    if (item.name == 'liuyan') {
      self.setData({
        liuyan: e.detail.value
      })
    }
    console.log(e)
  },
  /**
   * 提交售后申请
   */
  returnGoods(){
    var data = {};
    data.order_id = this.data.returndata.goods[0].order_id;
    data.rec_id = this.data.returndata.goods[0].rec_id;
    data.reason = this.data.reason;
    data.returntype = this.data.returntype;
    data.goods_number = this.data.num;
    data.question = this.data.question;
    data.apply = this.data.liuyan;
    var img = this.data.imgs;
    var imgs = "";
    if(img.length > 0){
      img.map(res => {
        imgs += "," + res;
      })
    }
    data.image = imgs;
    if(!data.reason){
      showToast('请选择原因','warning');
      return;
    }
    if (!data.question) {
      showToast('请描述问题', 'warning');
      return;
    }
    AddReturnGoods(data).then(res=>{
      console.log(res)
      if(res.error_code == 10000){
        showToast(res.msg,'success');
      }else{
        showToast(res.msg, 'warning');
        return;
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})